# -*- coding: utf-8 -*-

#
# peerscape.http.controllers.index
#
# Copyright 2008 Helsinki Institute for Information Technology
# and the authors.
#
# Authors:
#       Konrad Markus <konrad.markus@hiit.fi>
#

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from webob import Response
from webob.exc import HTTPMovedPermanently, HTTPTemporaryRedirect, HTTPNotFound
from peerscape.http.controllers import BaseController

class Index(BaseController):
    def index(self, req):
        db = req.environ['peerscape.db']

        # make sure we have a persona
        try:
            my_iid = db.get_my_iid()
            db.blocking_get(my_iid, '/info/modified')
        except LookupError: # persona not initialized
            # redirect to admin
            redir_url = 'http://admin.hexlabel.net/setup/'
            res = HTTPTemporaryRedirect(location=redir_url)
            res.cache_expires(10)
            return res

        # determine the kind of dataset and redirect
        if req.urlvars.has_key('peerscape.hexlabel'):
            type = db.get_dataset_type(req.urlvars['peerscape.hexlabel'])
            if type:
                if type == 'group':
                    redir_url = 'http://%s.hexlabel.net/group/' % req.urlvars['peerscape.hexlabel']
                    return HTTPMovedPermanently(location=redir_url)
                elif type == 'person' or type == 'principal':
                    redir_url = 'http://%s.hexlabel.net/person/' % req.urlvars['peerscape.hexlabel']
                    return HTTPMovedPermanently(location=redir_url)
                elif type == 'application':
                    redir_url = 'http://%s.hexlabel.net/application/' % req.urlvars['peerscape.hexlabel']
                    return HTTPMovedPermanently(location=redir_url)
                elif type == 'content':
                    redir_url = 'http://%s.hexlabel.net/content/' % req.urlvars['peerscape.hexlabel']
                    return HTTPMovedPermanently(location=redir_url)

            else:
                #[TODO: no type?]
                pass

        if req.host == 'my.hexlabel.net': 
            redir_url = 'http://%s.hexlabel.net/person/' % my_iid
            res = HTTPTemporaryRedirect(location=redir_url)
            res.cache_expires(10)
            return res

        else:
            return HTTPNotFound()


